<template>
  <div>
    <el-button
      type="primary"
      style="margin-top: 10px"
      @click="showAddress = true"
      >添加地址</el-button
    >
    <el-dialog title="添加地址" :visible.sync="showAddress" width="40%">
      <span
        ><el-card class="filter-container" shadow="never">
          <el-form
            :inline="true"
            size="small"
            :model="addData"
            ref="addData"
            :rules="rules"
            label-width="140px"
          >
            <el-form-item label="省份:" prop="provinceId">
              <el-select
                v-model="addData.provinceId"
                style="width: 203px"
                placeholder="请选择地址所在省份"
                @change="showCity"
                clearable
              >
                <el-option
                  v-for="(item, index) in provinceOptions"
                  :key="index"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="城市:" prop="cityId">
              <el-select
                v-model="addData.cityId"
                style="width: 203px"
                placeholder="请选择地址所在城市"
                @change="showArea"
                clearable
              >
                <el-option
                  v-for="(item, index) in cityOptions"
                  :key="index"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="地区:" prop="areaId">
              <el-select
                @change="selectUpdate"
                v-model="addData.areaId"
                style="width: 203px"
                placeholder="请选择地址所在地区"
                clearable
              >
                <el-option
                  v-for="(item, index) in areaOptions"
                  :key="index"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item prop="name" label="用户名:">
              <el-input
                style="width: 300px"
                v-model="addData.name"
                placeholder="请描述您的用户名"
                clearable
              ></el-input>
            </el-form-item>

            <el-form-item prop="address" label="详细地址:">
              <el-input
                style="width: 300px"
                v-model="addData.address"
                placeholder="请描述您的详细地址"
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item prop="email" label="邮箱地址:">
              <el-input
                style="width: 300px"
                v-model="addData.email"
                placeholder="请描述您的邮箱地址"
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item prop="phone" label="联系电话:">
              <el-input
                style="width: 300px"
                v-model="addData.phone"
                placeholder="请描述您的联系电话"
                clearable
              ></el-input>
            </el-form-item>
          </el-form> </el-card
      ></span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showAddress = false">取 消</el-button>
        <el-button type="primary" @click="(showAddress = false), addAddress()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-table
      v-loading="listLoading"
      :data="tableData"
      border
      style="width: 90%; margin-top: 10px"
    >
      <el-table-column prop="id" label="地址ID" width="100" align="center">
      </el-table-column>
      <el-table-column prop="name" label="用户名" width="180" align="center">
      </el-table-column>
      <el-table-column prop="phone" label="电话" width="180" align="center">
      </el-table-column>
      <el-table-column
        prop="email"
        label="邮箱"
        width="180"
        align="center"
      ></el-table-column>
      <el-table-column prop="address" label="地址" width="300" align="center">
      </el-table-column>
      <el-table-column label="操作" width="175" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="deletAddress(scope.row.id)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @current-change="getAddress"
        layout="prev, pager, next"
        :page-count="pageCunt"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "adminHome",
  data() {
    return {
      searchList: {
        order_id: "",
        commany_id: "",
        status: "",
      },
      tableData: [],
      addData: {},
      provinceOptions: [],
      cityOptions: [],
      areaOptions: [],
      lable: { lableNames: [] },
      sendMessage: {
        address: "",
        Address: "",
        labelId: "",
        person: "",
        price: "",
        time: "",
      },
      pageCunt: 1,
      currentpage:1,
      listLoading: true,
      showAddress: false,
      rules: {
        provinceId: [
          { required: true, message: "请输入省份", trigger: "blur" },
        ],
        cityId: [{ required: true, message: "请输入城市", trigger: "blur" }],
        address: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
        areaId: [{ required: true, message: "请输入地区", trigger: "blur" }],
        email: [{ required: true, message: "请输入邮箱地址", trigger: "blur" }],
        name: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        phone: [{ required: true, message: "请输入联系电话", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getAddress(1);
    this.getProvince();
  },
  methods: {
    getAddress(pageNum) {
      this.currentpage = pageNum
      this.listLoading = true;
      this.axios
        .get("api/user/auth/address/list?pageNum=" + pageNum + "&pageSize=5")
        .then((response) => {
          this.tableData = response.data.value.data;
          if (pageNum == 1) {
            if (response.data.value.total > 5) var total = 5;
            else var total = response.data.value.total;
          } else {
            var temp = response.data.value.total - (pageNum - 1) * 5;
            if (temp > 5) var total = 5;
            else var total = temp;
          }
          for (var i = 0; i < total; i++) {
            this.tableData[i].address = response.data.value.data[i].area.city.province.name + response.data.value.data[i].area.city.name + response.data.value.data[i].area.name + this.tableData[i].address
          }
          if (
            response.data.value.total / 5 >
            Number.parseInt(response.data.value.total / 5)
          ) {
            this.pageCunt = Number.parseInt(response.data.value.total / 5) + 1;
          } else {
            this.pageCunt = Number.parseInt(response.data.value.total / 5);
          }
          this.listLoading = false;
        })
        .catch((error) => {});
    },
    getProvince() {
      this.axios
        .get("api/user/sys/listProvince")
        .then((response) => {
          this.provinceOptions = response.data.value;
        })
        .catch((error) => {});
    },
    showCity(item) {
      this.addData.areaId = "";
      this.addData.cityId = "";
      this.cityOptions = [];
      this.areaOptions = [];
      if (item != 0) {
        this.axios
          .get("api/user/sys/listCity?provinceId=" + item)
          .then((response) => {
            this.cityOptions = response.data.value;
          })
          .catch((error) => {});
      }
    },
    showArea(item) {
      this.addData.areaId = "";
      this.areaOptions = [];
      if (item != 0) {
        this.axios
          .get("api/user/sys/listArea?cityId=" + item)
          .then((response) => {
            this.areaOptions = response.data.value;
          })
          .catch((error) => {});
      }
    },
    deletAddress(id) {
      this.$confirm("是否确认删除该地址?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios
            .post("api/user/auth/address/deleteById?id=" + id)
            .then((response) => {
              if (response.data.code == "200") {
                this.$message({
                  message: "删除成功",
                  type: "success",
                });
                this.getAddress(this.currentpage);
              }
            })
            .catch((error) => {});
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消该操作",
          });
        });
    },
    addAddress() {
      this.$refs.addData.validate((valid) => {
        if (valid) {
          this.axios
            .post(
              "api/user/auth/address/insert?address=" +
                this.addData.address +
                "&areaId=" +
                this.addData.areaId +
                "&email=" +
                this.addData.email +
                "&name=" +
                this.addData.name +
                "&phone=" +
                this.addData.phone
            )
            .then((response) => {
              if (response.data.code == 200) {
                this.$message.success("添加成功");
                this.getAddress(this.currentpage);
              }
            })
            .catch((error) => {
              this.$message.error(error);
            });
        } else {
          this.$message.error("请填写全部内容");
          return false;
        }
      });
    },
    selectUpdate(e) {
      this.$forceUpdate();
    },
  },
};
</script>